<?php
/**
 * Created by PhpStorm
 * User: whh
 * Date: 2022/8/25
 * Time: 17:00
 */

namespace app\backstage\controller;


use app\backstage\logic\PowerLogic;
use app\backstage\logic\UserLogic;
use app\backstage\model\ManagerRole;
use app\backstage\model\PowerNode;
use think\App;
use think\Controller;

class Power extends Controller
{
    protected $middleware = [
        'VerifyJwt' => ['except' => []],
        'VerifyPower' => ['except' => []],
    ];

    protected $logic;

    /**
     * 容器写法
     * Power constructor.
     * @param PowerLogic|null $logic
     */
    public function __construct(PowerLogic $logic = null)
    {
        $this->logic = $logic;
        parent::__construct();
    }

    /**
     * 角色页面
     * @return mixed
     * @Author: whh
     */
    public function role()
    {
        $list = ManagerRole::all();
        $this->assign('list', $list);
        return $this->fetch();
    }


    /**
     * 节点页面
     * @return mixed
     * @Author: whh
     */
    public function node()
    {
        return $this->fetch();
    }


    /**
     * 用户节点页面
     * @return mixed
     * @Author: whh
     */
    public function userNode()
    {
        $id = input('id');
        $role = ManagerRole::get($id);
        if (!$role) {
            $this->error('角色不存在');
        }
        $this->assign('role', $role);
        return $this->fetch();
    }


    /**
     * 节点数据
     * @return \think\response\Json
     * @Author: whh
     */
    public function nodeData()
    {
        $role_id = input('role_id');
        $powerLogic = $this->logic;
        if ($role_id) {
            $role = ManagerRole::get($role_id);
            if (!$role) {
                $this->error('角色不存在');
            }
            $treeNodes = $powerLogic->treeNodes($role);
        } else {
            $treeNodes = $powerLogic->treeNodes();
        }
        return sucJson($treeNodes);
    }


    /**
     * 添加节点
     * @return \think\response\Json
     * @Author: whh
     */
    public function addNode()
    {
        $params = $this->request->param();
        $result = $this->validate($params, 'app\backstage\validate\PowerValidate.addNode');
        if (true !== $result) {
            $this->error($result);
        }
        $id = $this->logic->addNode($params['parent_node_id']);
        return sucJson($id);
    }

    /**
     * 编辑节点
     * @Author: whh
     */
    public function upNode()
    {
        $params = $this->request->param();
        $result = $this->validate($params, 'app\backstage\validate\PowerValidate.upNode');
        if (true !== $result) {
            $this->error($result);
        }
        $logic = $this->logic;
        $logic->upNode($params);
        $this->success();
    }


    /**
     * 删除节点
     * @return \think\response\Json
     * @throws \Exception
     * @Author: whh
     */
    public function delNode()
    {
        $params = $this->request->param();
        $result = $this->validate($params, 'app\backstage\validate\PowerValidate.delNode');
        if (true !== $result) {
            $this->error($result);
        }
        $logic = $this->logic;
        return $logic->delNode($params['id']);
    }


    /**
     * 授权节点
     * @return \think\response\Json
     * @Author: whh
     */
    public function authNode(){
        $params = $this->request->param();
        $result = $this->validate($params, 'app\backstage\validate\PowerValidate.authNode');
        if (true !== $result) {
            $this->error($result);
        }
        return $this->logic->authNode($params['role_id'], $params['id'], $params['checked']);
    }


    /**
     * 编辑角色
     * @return mixed
     * @Author: whh
     */
    public function roleForm()
    {
        $id = input('id');
        $role = ManagerRole::get($id);
        if (!$role) {
            echo "角色不存在";
            die;
        }
        $this->assign('role', $role);
        return $this->fetch();
    }


    /**
     * 修改角色
     * @Author: whh
     */
    public function editRole()
    {
        $params = $this->request->param();
        $result = $this->validate($params, 'app\backstage\validate\PowerValidate.editRole');
        if (true !== $result) {
            $this->error($result);
        }
        $id = input('id');
        $role = ManagerRole::get($id);
        if (!$role) {
            $this->error('角色不存在');
        }
        $role->allowField(['title', 'status'])->save($params);
        $this->success();
    }

}